package com.matt.controller.api.web;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject;
import com.matt.commons.utils.JsonUtil;
import com.matt.foundation.model.StatusResponse;
import org.junit.Assert;
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
import org.springframework.boot.test.json.GsonTester;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import java.util.Map;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author diao.qiao@outlook.com
 * @version V1.0
 * @description: MockMvc测试工具类
 * @since 2019-01-14
 */
public class MockMvcUtil {

    /**
     * controller 测试工具，不含请求头
     *
     * @param wac          WebApplicationContext
     * @param url          请求URL
     * @param params       请求参数
     * @param expectedCode 预期返回的自定义code
     */
    static void test(WebApplicationContext wac, String url, MultiValueMap params, String expectedCode) {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        Map map = null;
        try {
            String content = mockMvc.perform(
                    post(url)
                            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                            .params(params)
            ).andExpect(status().isOk()
            ).andDo(print()).andReturn().getResponse().getContentAsString();
            //response = JSONObject.parseObject(content, StatusResponse.class);
            map = JsonUtil.readToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Assert.assertEquals("Test失败", expectedCode, map.get("code"));
        }
    }

    /**
     * controller 测试工具，含请求头
     *
     * @param wac          WebApplicationContext
     * @param url          请求URL
     * @param headers      请求头
     * @param params       请求参数
     * @param expectedCode 预期返回的自定义code
     */
    static void testAndHeader(WebApplicationContext wac, String url, HttpHeaders headers, MultiValueMap params, String expectedCode) {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        Map map = null;
        try {
            String content = mockMvc.perform(
                    post(url)
                            .headers(headers)
                            .params(params)
            ).andExpect(status().isOk())
                    .andDo(print())
                    .andReturn().getResponse().getContentAsString();
            //response = JSONObject.parseObject(content, StatusResponse.class);
            map = JsonUtil.readToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Assert.assertEquals("Test失败", expectedCode, map.get("code"));
        }
    }

}
